package net.retorx

import javax.ws.rs._
import collection.JavaConversions.JMapWrapper
import com.google.inject.{Inject, Singleton}

@Path("/admin")
@Singleton
class AdminImageContentService @Inject() (imageContentDAO:ImageContentDAO) {

    def successJson = """{"success":true}"""

    @POST
    @Path("rename/{tag}")
    @Consumes(Array("text/json", "application/json", "application/vnd.imageContent+json"))
    @Produces(Array("text/json"))
    def renameTag(@PathParam("tag") existingTag: String, newTag: String) = {
        imageContentDAO.renameTag(existingTag, newTag)
        successJson
    }

    @POST
    @Path("{id}/properties")
    @Consumes(Array("text/json", "application/json", "application/vnd.imageContent+json"))
    @Produces(Array("application/vnd.imageContent+json"))
    def saveProperties(@PathParam("id") id: String, properties: java.util.Map[String, String]) = {
        val wrapped = new JMapWrapper[String, String](properties)
        val imageContent = imageContentDAO.getImageContent(id)
        imageContent.setProperties(wrapped)
        imageContentDAO.savePropertiesFile(imageContent)
        // a little overzealous, but whatever - how long does it take to determine whether tags have changed vs
        // just reloading the damned tags?
        imageContentDAO.reloadTags()
        successJson
    }

    @GET
    @Path("/reloadTags")
    def reloadTags() = {
        imageContentDAO.reloadTags()
        successJson
    }

    @GET
    @Path("/reloadFromFiles")
    def reloadFromFiles() = {
        imageContentDAO.reloadFromFiles()
        successJson
    }
}